package com.qiudaozhang.controller;

import com.qiudaozhang.dto.ResponseCode;
import com.qiudaozhang.model.GoodsInfo;
import com.qiudaozhang.model.User;
import com.qiudaozhang.service.GoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;



    @RequestMapping("to/list")
    public String toList(){
        return  "goods/list";
    }

    @RequestMapping("to/add")
    public String toAdd(){
        return  "goods/add";
    }
    @RequestMapping("to/modify")
    public String toModify(@Param("id") Long id,Model model){
        GoodsInfo goodsInfo=goodsService.queryById(id);
        model.addAttribute("goodsInfo",goodsInfo);
        return  "goods/modify";
    }
    @RequestMapping("to/see")
    public String toSee(@Param("id") Long id,Model model){
        GoodsInfo goodsInfo=goodsService.queryById(id);
        model.addAttribute("goodsInfo",goodsInfo);
        return  "goods/see";
    }
    @RequestMapping("modify")
    public String modify(GoodsInfo goodsInfo){
        goodsService.modifyById(goodsInfo);
        return  "redirect:to/list";
    }

    @RequestMapping("del/{id}")
    @ResponseBody
    public ResponseCode del(@PathVariable("id") Long id){
        ResponseCode code = goodsService.delById(id);
        return code;
    }
    @RequestMapping("add")
    public String add(GoodsInfo goodsInfo, HttpSession session){
        User user= (User) session.getAttribute("sessionUser");
        goodsInfo.setCreatedby(user.getLoginCode());
        goodsService.add(goodsInfo);
        return  "redirect:to/list";
    }


    @RequestMapping("list")
    @ResponseBody
    public ResponseCode list(@Param("goodsName") String goodsName){
        ResponseCode code= goodsService.query(goodsName);
        return  code;
    }



}
